package com.panda.delivery.activity;


import com.panda.delivery.R;
import com.facebook.drawee.backends.pipeline.Fresco;
import com.facebook.drawee.backends.pipeline.PipelineDraweeControllerBuilder;
import com.facebook.drawee.controller.BaseControllerListener;
import com.facebook.imagepipeline.image.ImageInfo;
import com.panda.delivery.view.MultiTouchViewPager;

import android.graphics.drawable.Animatable;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;

import androidx.fragment.app.FragmentActivity;
import androidx.viewpager.widget.PagerAdapter;

import java.util.ArrayList;

//import me.relex.circleindicator.CircleIndicator;
import me.relex.circleindicator.CircleIndicator;
import me.relex.photodraweeview.PhotoDraweeView;


/**
 * Description: 大图浏览
 * 支持左右滑动切换
 * 要先初始化fresco Fresco.initialize(this);
 */
public class LookPicActivity extends FragmentActivity
{

    private ArrayList<String> mDataList;
    private int currentPosition;

    @SuppressWarnings("unchecked")
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        //全屏
        getWindow().setFlags(WindowManager.LayoutParams. FLAG_FULLSCREEN ,
                WindowManager.LayoutParams. FLAG_FULLSCREEN);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_look_pic);

        currentPosition = getIntent().getIntExtra("position", 0);
        mDataList = getIntent().getStringArrayListExtra("pictures");


        CircleIndicator indicator = (CircleIndicator) findViewById(R.id.indicator);
        MultiTouchViewPager viewPager = (MultiTouchViewPager) findViewById(R.id.view_pager);
        viewPager.setAdapter(new DraweePagerAdapter());
        indicator.setViewPager(viewPager);
        viewPager.setCurrentItem(currentPosition);

        if (mDataList != null)
        {
            if (mDataList.size() == 1)
            {
                indicator.setVisibility(View.GONE);
            }
        }
    }

    public class DraweePagerAdapter extends PagerAdapter
    {

        @Override
        public int getCount()
        {
            return mDataList == null ? 0 : mDataList.size();
        }

        @Override
        public boolean isViewFromObject(View view, Object object)
        {
            return view == object;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object)
        {
            container.removeView((View) object);
        }

        @Override
        public Object instantiateItem(ViewGroup viewGroup, int position)
        {
            final PhotoDraweeView photoDraweeView = new PhotoDraweeView(viewGroup.getContext());

            PipelineDraweeControllerBuilder controller = Fresco.newDraweeControllerBuilder();
            String url = mDataList.get(position).trim();
            if(!url.endsWith(".gif"))
            {
                controller.setUri(Uri.parse(url));
            }
            controller.setOldController(photoDraweeView.getController());
            controller.setControllerListener(new BaseControllerListener<ImageInfo>()
            {
                @Override
                public void onFinalImageSet(String id, ImageInfo imageInfo, Animatable animatable)
                {
                    super.onFinalImageSet(id, imageInfo, animatable);
                    if (imageInfo == null)
                    {
                        return;
                    }
                    photoDraweeView.update(imageInfo.getWidth(), imageInfo.getHeight());
                }
            });
            photoDraweeView.setController(controller.build());

            try
            {
                viewGroup.addView(photoDraweeView, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
            }
            catch (Exception e)
            {
                e.printStackTrace();
            }
            photoDraweeView.setOnPhotoTapListener((view, x, y) -> finish());
            return photoDraweeView;
        }
    }
}
